package com.wombatsoft.gtw;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.wombatsoft.gtw.xmpp.ConnectionManager;

public class MainActivity extends Activity {

	private TextView status;
	private Button btnLogin;
	private Button btnContacts;
	//private static ProgressBar pbLogin;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		btnLogin = (Button) findViewById(R.id.btnLogin);
		btnContacts = (Button) findViewById(R.id.btnBuddyList);
		EditText txfUser = (EditText) findViewById(R.id.txfUser);
		EditText txfPass = (EditText) findViewById(R.id.txfPassword);
		status = (TextView) findViewById(R.id.txvStatus);
		//pbLogin = (ProgressBar) findViewById(R.id.pbLogin);
		//pbLogin.setVisibility(View.GONE);
		
		btnLogin.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Button clickedButton = (Button) arg0;
				Log.d("MainActivity", String.format("Button clicked: %s", clickedButton.getText()));
				
				if (clickedButton.getId() == btnLogin.getId() &&
				    "Login".equals(btnLogin.getText())) {
					EditText txfUser = (EditText) findViewById(R.id.txfUser);
					EditText txfPass = (EditText) findViewById(R.id.txfPassword);
					String user = txfUser.getText().toString();
					String pass = txfPass.getText().toString();
					new LoginTask().execute(user, pass);
				} else if (clickedButton.getId() == btnLogin.getId() &&
						"Logout".equals(btnLogin.getText())) {
					new LogoutTask().execute();
				}
			}
		});
		
		btnContacts.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Button clickedButton = (Button) arg0;
				Log.d("MainActivity", String.format("Button clicked: %s", clickedButton.getText()));
				
				if (clickedButton.getId() == btnContacts.getId()) {
					Intent in = new Intent(MainActivity.this, ContactListActivity.class);
					startActivity(in);
				}
			}
		});

	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public class LoginTask extends AsyncTask<String, Boolean, Boolean>{
		
		 @Override
	     protected void onPostExecute(Boolean result) {
	         super.onPostExecute(result);
	         //pbLogin.setVisibility(View.GONE);
	         if (Boolean.TRUE.equals(result)) {
	        	 btnLogin.setText("Logout");
	         }
	     }

	     @Override
	     protected void onPreExecute() {
	         super.onPreExecute();
	         //pbLogin.setVisibility(View.GONE);
	     }

		@Override
		protected Boolean doInBackground(String... params) {
			String user = params[0];
	        String password = params[1];
	        
	        return ConnectionManager.getInstance().login(user, password);
	        
		}

	}
	
	public class LogoutTask extends AsyncTask<Void, Void, Void>{
		
	     @Override
	     protected void onPreExecute() {
	         super.onPreExecute();
	     }

		@Override
		protected Void doInBackground(Void... params) {
			ConnectionManager.getInstance().logout();
			return null;
		}
		
		@Override
		protected void onPostExecute(Void param) {
			btnLogin.setText("Login");
		}

	}
}
